import { _decorator, Color, Component, Graphics, Label, Mask, Node, Tween, tween, UITransform, v3 } from 'cc';
import Ab_GameDC_bA from '../../manager/data.game.center';
import { Ab_AudioManager_bA } from '../../manager/audio-manager';
const { ccclass, property } = _decorator;

/** 引导的完成类型（指示引导是如何被完成的） */
enum Ab_DoneType_bA {
	/** 点击完成 */
	Ab_TOUCH_bA,
	/** 延时完成 */
	Ab_DELAY_bA,
	/**  */
	Ab_DONE_bA,
}

@ccclass('Ab_GameGuide_bA')
export class Ab_GameGuide_bA extends Component {

	@property(Mask)
	private Ab_mask_bA: Mask = null;
	@property(UITransform)
	private Ab_toucher_bA: UITransform;
	@property(UITransform)
	private Ab_pointer_bA: UITransform = null;
	@property(UITransform)
	private Ab_content_bA: UITransform = null;
	@property(Label)
	private Ab_lbl_content_bA: Label = null;

	private Ab_uitrans_bA: UITransform;
	private Ab_mask_graphic_bA: Graphics;
	private Ab_mask_uitrans_bA: UITransform;

	public static Ab_DoneType_bA = Ab_DoneType_bA;

    protected start() {
		if([1, 2, 3, 4].every(id=>Ab_GameDC_bA.Ab_checkGuideID_bA(id))){
			this.node.removeFromParent();
			this.node.destroy();
		}else{
			Ab_GameGuide_bA.Ab_ins_bA = this;
			this.Ab_init_bA();
			if(Ab_GameGuide_bA.Ab_show_step_bA!=undefined) this.Ab_show_bA(Ab_GameGuide_bA.Ab_show_step_bA.step);
		}

		Ab_GameDC_bA.Ab_Status_bA.Ab_guideInited_bA = true;
    }

	private Ab_init_bA(){
		this.Ab_uitrans_bA = this.node.getComponent(UITransform);
		this.Ab_mask_graphic_bA = this.Ab_mask_bA.node.getComponent(Graphics);
		this.Ab_mask_uitrans_bA = this.Ab_mask_bA.node.getComponent(UITransform);
		this.Ab_toucher_bA.node.on(Node.EventType.TOUCH_END, this.Ab_touchEnd_bA);

		this.node.active = false;
	}

	private Ab_showContent_bA(content: string, target: UITransform){
		this.Ab_content_bA.node.active = true;
		this.Ab_lbl_content_bA.string = content;

		let position = target ? this.Ab_uitrans_bA.convertToNodeSpaceAR(target.node.worldPosition) : v3(0, 0, 0);
		let va = new abi.Ab_cc_bA.Ab_VecAssist_bA(this.Ab_content_bA.node);

		if(position.x<0) position.x = Math.max(position.x, va.Ab_left_bA);
		else if(position.x > 0) position.x = Math.min(position.x, va.Ab_right_bA);

		if(target){
			if(position.y<0) position.y += (1-target.anchorY) * target.height + this.Ab_content_bA.height * 0.5 + 50;
			else position.y -= target.anchorY * target.height + this.Ab_content_bA.height * 0.5 + 50;
		}

		Tween.stopAllByTarget(this.Ab_content_bA.node);
		tween(this.Ab_content_bA.node)
		.to(0.25, {position: v3(position.x, position.y, this.Ab_content_bA.node.position.z)})
		.start();
	}

	/**
	 * 显示高亮区域
	 * @param list
	 */
	private Ab_showLight_bA(...list: UITransform[]){
		this.Ab_mask_graphic_bA.clear();
		this.Ab_mask_graphic_bA.lineWidth = 1;
		this.Ab_mask_graphic_bA.fillColor = Color.BLACK;

		if(list.length>0){
			let position = v3();
			list.forEach(target=>{
				this.Ab_mask_uitrans_bA.convertToNodeSpaceAR(target.node.worldPosition, position);
				this.Ab_mask_graphic_bA.rect(
					position.x - target.anchorX * target.width,
					position.y - target.anchorY * target.height,
					target.width, target.height
				);
				this.Ab_mask_graphic_bA.fill();
			});
		}else{
			this.Ab_mask_graphic_bA.rect(0, 0, 0, 0);
			this.Ab_mask_graphic_bA.fill();
		}
	}

	/**
	 * 显示引导动画(小手的动画)
	 * @param target
	 */
	private Ab_showPointer_bA(target: UITransform){
		let position = this.Ab_uitrans_bA.convertToNodeSpaceAR(target.node.worldPosition);

		position.x += (0.5-target.anchorX) * target.width;
		position.y += (0.5-target.anchorY) * target.height;

		Tween.stopAllByTarget(this.Ab_pointer_bA.node);
		this.Ab_pointer_bA.node.active = true;
		this.Ab_pointer_bA.node.setPosition(position.x, position.y, this.Ab_pointer_bA.node.position.z);

		let ratio = 1 / 3;
		let distance1 = v3(this.Ab_pointer_bA.width * ratio, this.Ab_pointer_bA.height * -ratio, 0);
		let distance2 = v3(this.Ab_pointer_bA.width * -ratio, this.Ab_pointer_bA.height * ratio, 0);
		tween(this.Ab_pointer_bA.node)
		.by(0.15, {position: distance1})
		.by(0.15, {position: distance2})
		.union().repeat(3).delay(0.8)
		.union().repeatForever().start();
	}

	private Ab_refreshToucher_bA(target: UITransform){
		let position = this.Ab_uitrans_bA.convertToNodeSpaceAR(target.node.worldPosition);

		position.x += (0.5-target.anchorX) * target.width;
		position.y += (0.5-target.anchorY) * target.height;

		this.Ab_toucher_bA.node.setPosition(position.x, position.y, this.Ab_toucher_bA.node.position.z);
		this.Ab_toucher_bA.setContentSize(target.contentSize);
	}

	private Ab_show_bA(step: Ab_GuideStep_bA){

		this.node.active = true;

		if(step.Ab_content_bA) this.Ab_showContent_bA(step.Ab_content_bA, step.Ab_light_target_bA??step.Ab_pointer_target_bA);
		else this.Ab_content_bA.node.active = false;
		if(step.Ab_pointer_target_bA) this.Ab_showPointer_bA(step.Ab_pointer_target_bA);
		else this.Ab_pointer_bA.node.active = false;

		let ui_list = [step.Ab_light_target_bA, step.Ab_pointer_target_bA].filter(ui=>Boolean(ui));
		if(ui_list.length>0) this.Ab_showLight_bA(...ui_list);
		else this.Ab_mask_graphic_bA.clear();

		this.Ab_refreshToucher_bA(step.Ab_pointer_target_bA??this.Ab_uitrans_bA);

		step.Ab_npromise_show_bA.Ab_resolve_bA(true);
	}

	private Ab_close_bA(){
		Tween.stopAllByTarget(this.Ab_pointer_bA.node);
		this.node.active = false;
	}

	private Ab_touchEnd_bA(){
		Ab_GameGuide_bA.Ab_Done_bA(Ab_GameGuide_bA.Ab_show_step_bA.id, Ab_DoneType_bA.Ab_TOUCH_bA);
	}

	/** 引导步骤记录 */
	private static Ab_step_map_bA = new Map<number, Ab_GuideStep_bA>();
	/** 当前正在显示的引导数据 */
	private static Ab_show_step_bA: {id: number, step: Ab_GuideStep_bA};
	private static Ab_ins_bA: Ab_GameGuide_bA;
	private static Ab_wait_close_bA = new Map<number, boolean>();

	/**
	 * 创建一个引导步骤
	 * @param id
	 */
	public static Ab_Create_bA(id: number, Ab_content_bA?: string, Ab_light_target_bA?: UITransform, Ab_pointer_target_bA?: UITransform){
		let step = Ab_GameGuide_bA.Ab_step_map_bA.get(id);
		if(!step){
			step = {
				Ab_npromise_show_bA: new abi.Ab_NudityPromise_bA(),
				Ab_npromise_done_bA: new abi.Ab_NudityPromise_bA(),
			};
			Ab_GameGuide_bA.Ab_step_map_bA.set(id, step);
			Ab_GameGuide_bA.Ab_wait_close_bA.set(id, true);
		};

		Object.assign(step, {Ab_content_bA, Ab_light_target_bA, Ab_pointer_target_bA});
		if(Ab_GameDC_bA.Ab_checkGuideID_bA(id)){
			step.Ab_inited_bA = true;
			step.Ab_npromise_show_bA.Ab_resolve_bA(true);
			Ab_GameGuide_bA.Ab_Done_bA(id, Ab_DoneType_bA.Ab_DONE_bA);
		}

		if(!step.Ab_inited_bA) step.Ab_inited_bA = ![Ab_content_bA, Ab_light_target_bA, Ab_pointer_target_bA].map(v=>v==undefined).every(v=>v==true);
		return step;
	}

	/**
	 * 显示一个引导步骤
	 * @param id 引导步骤的id
	 */
	public static Ab_Show_bA(id: number, delay?: number){
		if(Ab_GameGuide_bA.Ab_show_step_bA != undefined){
			console.error('当前有正在显示中的引导，显示逻辑冲突' + Ab_GameGuide_bA.Ab_show_step_bA);
			return void 0;
		}
		if(!Ab_GameGuide_bA.Ab_step_map_bA.has(id)) throw TypeError('Create a guide step with id: '+id+' before showing it!!!');

		let step = Ab_GameGuide_bA.Ab_step_map_bA.get(id);
		if(step.Ab_done_bA) return step.Ab_npromise_show_bA.Ab_promise_bA;
		if(step.Ab_inited_bA){
			Ab_GameGuide_bA.Ab_show_step_bA = {id, step};
			if(Ab_GameGuide_bA.Ab_ins_bA) Ab_GameGuide_bA.Ab_ins_bA.Ab_show_bA(step);
			if(delay) abi.Ab_delay_bA(delay, ()=>Ab_GameGuide_bA.Ab_Done_bA(id, Ab_DoneType_bA.Ab_DELAY_bA));
			return step.Ab_npromise_show_bA.Ab_promise_bA;
		}else{
			console.error('未初始化的引导步骤，请在引导步骤显示之前，进行初始化配置', step);
		}
	}

	/**
	 * 完成一个引导步骤
	 * @param id 引导步骤的id
	 */
	public static Ab_Done_bA(id: number, type: Ab_DoneType_bA){
		if(!Ab_GameGuide_bA.Ab_step_map_bA.has(id)) throw ReferenceError('No guide step with id: '+id+' is found. Please confirm whether it was created!!!');
		let step = Ab_GameGuide_bA.Ab_step_map_bA.get(id);
		step.Ab_done_bA = true;

		if(Ab_GameGuide_bA.Ab_show_step_bA && Ab_GameGuide_bA.Ab_show_step_bA.id==id){
			Ab_GameGuide_bA.Ab_show_step_bA = undefined;
			let close = Ab_GameGuide_bA.Ab_wait_close_bA.get(id);
			if(close && Ab_GameGuide_bA.Ab_ins_bA) Ab_GameGuide_bA.Ab_ins_bA.Ab_close_bA();
		}

		if(!Ab_GameDC_bA.Ab_checkGuideID_bA(id)) Ab_GameDC_bA.Ab_DoneGuidID_bA(id);

		if(type==Ab_DoneType_bA.Ab_TOUCH_bA) Ab_AudioManager_bA.Ab_playEffect_bA(Ab_AudioManager_bA.Ab_EAudio_bA.Ab_EF_CLICK_bA);
		step.Ab_npromise_done_bA.Ab_resolve_bA(type);
	}

	/**
	 * 等待一个引导步骤id
	 * @param id 引导步骤的id
	 */
	public static Ab_Wait_bA(id: number, close = true){
		let step = Ab_GameGuide_bA.Ab_step_map_bA.get(id);
		if(!step) step = Ab_GameGuide_bA.Ab_Create_bA(id);
		Ab_GameGuide_bA.Ab_wait_close_bA.set(id, close);
		return step.Ab_npromise_done_bA.Ab_promise_bA;
	}

	/**
	 * 判断是否存在指定id的引导步骤
	 * @param id 引导步骤的id
	 */
	public static Ab_Has_bA(id: number){
		return Ab_GameGuide_bA.Ab_step_map_bA.has(id);
	}

	/**
	 * 判断是否存在指定id的引导步骤
	 * @param id 引导步骤的id
	 */
	public static Ab_Get_bA(id: number){
		return Ab_GameGuide_bA.Ab_step_map_bA.get(id);
	}

	/**
	 * 清除指定id的引导步骤
	 * @param list
	 */
	public static Ab_CleanGuideStep_bA(...list: number[]){
		list.forEach(id=>{
			if(Ab_GameGuide_bA.Ab_step_map_bA.has(id)) Ab_GameGuide_bA.Ab_step_map_bA.delete(id);
			if(Ab_GameGuide_bA.Ab_show_step_bA && Ab_GameGuide_bA.Ab_show_step_bA.id==id) Ab_GameGuide_bA.Ab_show_step_bA = undefined;
		});
	}
}

/** 引导步骤数据结构 */
interface Ab_GuideStep_bA {
	/** 可供操作的promise */
	Ab_npromise_show_bA: abi.Ab_NudityPromise_bA<boolean>;
	Ab_npromise_done_bA: abi.Ab_NudityPromise_bA<Ab_DoneType_bA>;
	/** 引导文案 */
	Ab_content_bA?: string;
	/** 高亮区域 */
	Ab_light_target_bA?: UITransform;
	/** 指引区域（也就是小手的动画） */
	Ab_pointer_target_bA?: UITransform;
	/** 引导数据是否初始化 */
	Ab_inited_bA?: boolean;
	Ab_done_bA?: boolean;
	/** 引导是如何被完成的 */
	Ab_done_type_bA?: Ab_DoneType_bA;
};
